<?php

namespace Biz\service\NiceGoodsTopic;

use Biz\model\NiceGoodsTopic\GoodsList as mGoodsList;
use Biz\model\NiceGoodsTopic\SecondTopic;
use Biz\service\wiki\Wiki as sWiki;
use Doraemon\service\Format\Note\HandleNote;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\BlockType;
use Doraemon\tools\ImgUrl;


class GoodList
{

    const PAGESIZE = 15;

    public static function getGoodsList($topic_id, $page)
    {
        $goodsList = mGoodsList::getListByWhere([
            'rela_topic_id' => $topic_id,
            'LIMIT' => [($page - 1) * self::PAGESIZE, self::PAGESIZE],
            'goods_status' => mGoodsList::STATUS_NORMAL,
            'ORDER' => ['sort' => 'ASC', 'addtime' => 'DESC']
        ]);
        if (empty($goodsList)) return [];
        $return = [];
        if ($goodsList && is_array($goodsList)) {
            $objIds = array_column($goodsList, 'obj_id');
            $wikiIds = array_column($goodsList, 'wiki_id');
            $contentInfo = self::getConentInfo($objIds);
            $wikiInfo = self::getWikiInfo($wikiIds);
            foreach ($goodsList as $val) {
                if (empty($wikiInfo[$val['wiki_id']]) || empty($contentInfo[$val['obj_id']])) continue;
                $wikiInfo[$val['wiki_id']]['title'] = $val['goods_desc'] ?: $wikiInfo[$val['wiki_id']]['title'];
                $ObjInfo = $contentInfo[$val['obj_id']];
                $ObjInfo['remark'] = $val['content_desc'] ?: $ObjInfo['remark'];
                $return[] = [
                    'id' => $val['id'],
                    'wiki_info' => $wikiInfo[$val['wiki_id']],
                    'content_info' => $ObjInfo,
                    'statSign' => basicStatSign::genStatSign(basicStatSign::MODULE_FINE_GOODS_TOPIC_GOODS_LIST, [
                        basicStatSign::EXTEND_KEY_OBJ_ID => $val['obj_id'],
                    ], ['wiki_id' => $val['wiki_id'], 'obj_id' => $val['obj_id'], 'topic_id' => $topic_id, 'page' => $page]),
                ];

            }
            if ($page == 1 && $return) {
                $return[count($return) - 1]['rela_topic_list'] = self::getRelaTopic($topic_id);
            }

        }
        return $return;
    }


    public static function getConentInfo($obj_ids)
    {
        $noteInfo = HandleNote::handleNote($obj_ids, HandleNote::BASE_INFO);
        $result = [];
        if ($noteInfo && is_array($noteInfo)) {
            foreach ($noteInfo as $key => $val) {
                $userInfo = $val['photo']['user_info'];
                $result[$key]['avatar'] = $userInfo['small_avatar'];
                $result[$key]['nick'] = $userInfo['nick'];
                $result[$key]['remark'] = $val['photo']['photo_info']['remark'];
                $result[$key]['obj_id'] = $key;

            }
        }
        return $result;
    }

    public static function getWikiInfo($wikiIds)
    {
        $wikiInfo = sWiki::getWikiList($wikiIds, 0, sWiki::FIELDS_GROUP_BASE_LIST);;
        $result = [];
        if ($wikiInfo && is_array($wikiInfo)) {
            foreach ($wikiInfo as $key => $val) {
                $result[$key]['id'] = $val['id'];
                $result[$key]['basic_id'] = $val['basic_id'];
                $result[$key]["basic_title"] = $val['basic_title'];
                $result[$key]["basic_state"] = $val['basic_state'];
                $result[$key]["title"] = $val['title'];
                $result[$key]["ori_min_price"] = $val['ori_min_price'];
                $result[$key]["ori_max_price"] = $val['ori_max_price'];
                $result[$key]["actual_min_price"] = $val['actual_min_price'];
                $result[$key]["actual_max_price"] = $val['actual_max_price'];
                $result[$key]["cover_img"] = $val['cover_img'];
            }
        }
        return $result;
    }

    public static function getRelaTopic($topic_id)
    {
        $topicInfo = SecondTopic::getInfo($topic_id);
        if (empty($topicInfo)) return [];

        $list = SecondTopic::getRelaTopicByPid($topicInfo['pid'], $topic_id, ['id', 'topic_name', 'img']);
        if ($list && is_array($list)) {
            foreach ($list as $key => &$val) {
                $val['cover_img'] = ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $val['img'], ImgUrl::IMAGE_SIZE_ORI_NPHONE);
                $val['statSign'] = basicStatSign::genStatSign(basicStatSign::MODULE_FINE_GOODS_RELA_TOPIC, [
                    basicStatSign::EXTEND_KEY_OBJ_ID => $val['id'],
                    basicStatSign::EXTEND_KEY_INDEX => $key,
                ], ['topic_id' => $val['id'], 'index' => $key]);
            }
        }
        return $list;
    }

}